<template>
  <div class="process">
    <el-timeline v-if="list && list.length" :style="{height: `calc(100vh - ${isDrawer ? 96 : 257}px)`}" class="process-content">
      <template v-for="(item, index) in list" >
        <el-timeline-item
          :class="getClassName(item)"
          :key="index"
          :size="item.nodeStatus === 1 ? 'large': 'normal'"
          :color="item.nodeStatus === 0 || item.nodeStatus === 1? '#406EFF': '#DDE1EA'"
          hide-timestamp >
          <slot name="processItem">
            <div v-if="item.nodeStatus === -2" class="check-filed-item">
              <!-- 查看脱敏字段日志 -->
              <div v-if="item.checkUser && item.checkUser.avatar" class="avatar"><img :src="item.checkUser.avatar" alt=""></div>
              <div v-if="item.checkUser.userName" class="label">{{ item.checkUser.userName }}</div>
              <div class="label mr-2px">查看了</div>
              <div :title="item.fieldName" class="fiedlName">{{ item.fieldName }}</div>
              <div v-if="item.time" class="time">{{ item.time | format('YYYY/MM/DD HH:mm') }}</div>
            </div>
            <processItem
              v-else
              :items="item.items"
              :process-status="processStatus"
              :is-comment="item.nodeStatus === -1"
              :is-carbon-copy="item.nodeType === 3"
              :is-fly-notice="item.nodeType === 4"
              :carbon-copy-name="item.nodeName"
              :show-process-status="index === 0"
              :all-approver="allApprover"
              :is-creator="isCreator"
              :approval-module-type="approvalModuleType"
              :action-permissions="actionPermissions"
              :main-id="mainId"
              @onReplyComment="onReplyComment"
              @reloadComment="reloadComment"
            />
          </slot>
          <!-- 流程状态章 -->
          <div v-if="item.nodeStatus !== -2 && commentStatus !== COMMENT_STATUS.SHOW_COMMENT && index === getStampIndex" :class="`process-status process-status${processStatus}`" />
        </el-timeline-item>
      </template>
    </el-timeline>
    <div v-else :style="{height: `calc(100vh - ${isDrawer ? 96 : 257}px)`}" >
      <empty-placeholder style="height: 400px;" />
    </div>
    <div class="comment-header-box">
      <div class="comment-title flex-center" @click="showCommentDialog">
        <span style="margin-right: 4px">添加评论</span>
        <i class="el-icon-chat-dot-square"/>
      </div>
      <div class="comment-type">
        <template v-for="(item, index) in logActiveBtns">
          <div
            :key="item.name"
            :class="{ 'comment-type-active': commentStatus === item.activeStatus, 'comment-middle-item': index === 1 && commentStatus !== COMMENT_STATUS.HIDE_COMMENT }"
            class="comment-type-item"
            @click="handleLogStatusChange(item.activeStatus)"
          >{{ item.name }}</div>
        </template>
      </div>
    </div>
  </div>
</template>

<script>
import processItem from './processItem';
import EmptyPlaceholder from '@/components/EmptyPlaceholder';
import { PROCESS_NODE_STATUS, COMMENT_STATUS } from '@/utils/const/process';
import { format } from '@/filters/date';

export default {
  components: {
    processItem,
    EmptyPlaceholder
  },
  filters: { format },
  props: {
    list: {
      type: Array,
      default: () => []
    },
    processStatus: {
      type: Number,
      default: 0
    },
    commentStatus: {
      type: Number,
      default: COMMENT_STATUS.SHOW_ALL
    },
    isCreator: { // 是否为创建人
      type: Boolean,
      default: false
    },
    isDrawer: {
      type: Boolean,
      default: true
    },
    allApprover: {
      type: Array,
      default: () => []
    },
    approvalModuleType: { // 模块对应类型
      type: Number,
      default: null
    },
    actionPermissions: { // 自定义表单页面操作权限
      type: Object,
      default: () => ({})
    },
    mainId: { // 详情ID
      type: Number | String,
      default: ''
    }
  },
  data() {
    return {
      COMMENT_STATUS,
      //  0：结束 1：进行中 2：未审批
      classList: ['node-end', 'node-ing', 'node-unexecuted']
    };
  },
  computed: {
    getStampIndex() {
      let stampIndex = 0;
      for (let i = 0; i < this.list.length; i++) {
        if (this.list[i].items) {
          const items = this.list[i].items;
          if (items[0].status !== PROCESS_NODE_STATUS.PERSON_CHANGE) {
            stampIndex = i;
            break;
          }
        }
      }
      return stampIndex;
    },
    logActiveBtns() {
      return [
        { name: '全部', activeStatus: COMMENT_STATUS.SHOW_ALL },
        { name: '仅日志', activeStatus: COMMENT_STATUS.HIDE_COMMENT },
        { name: '仅评论', activeStatus: COMMENT_STATUS.SHOW_COMMENT }
      ];
    }
  },
  methods: {
    handleLogStatusChange(status) {
      this.$emit('onCommentStatusChange', status);
    },
    onReplyComment(commentData) {
      this.$emit('onReplyComment', commentData);
    },
    showCommentDialog() {
      this.$emit('showCommentDialog');
    },
    reloadComment() {
      this.$emit('reloadComment');
    },
    getClassName(item) {
      if (!item.nodeStatus && item.nodeStatus !== 0) return 'node-begin';
      if (item.nodeStatus === -1) {
        return 'node-comment';
      } else if (item.nodeStatus === -2) {
        return 'node-checkField';
      } else {
        return this.classList[item.nodeStatus];
      }
    }
  }
};
</script>

<style lang="scss" scoped>
%background {
  background-repeat: no-repeat;
  background-position: center;
  background-size: 100%;
}
.process {
  height: 100%;
  .process-content {
    overflow-y: auto;
  }
  .comment-header-box {
    height: 48px;
    width: calc(100% + 17px);
    transform: translateX(-12px);
    box-shadow: 2px -2px 5px 0 rgba(221,225,234,0.5);
    background-color: #fff;
    padding: 0 12px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    color: #666;
    overflow: hidden;
    border-bottom-left-radius: 8px;
    border-bottom-right-radius: 8px;
    .comment-title {
      font-size: 14px;
      > i {
        font-size: 16px;
        margin-top: 1px;
      }
    }
    .comment-title:hover {
      color: #406eff;
      cursor: pointer;
      i {
        color: #406eff !important;
      }
    }
    .comment-type {
      width: 176px;
      height: 32px;
      display: flex;
      align-items: center;
      justify-content: space-around;
      background-color: #f5f5f5;
      color: #333;
      border-radius: 4px;
      padding: 0 4px;
      .comment-type-item {
        text-align: center;
        line-height: 24px;
        flex: 1;
        height: 24px;
        border-radius: 4px;
        cursor: pointer;
        position: relative;
      }
      .comment-type-active {
        background-color: #fff;
        color: #406eff;
        z-index: 2;
      }
      .comment-middle-item {
        &::before {
          content: '|';
          position: absolute;
          right: 56px;
          color: #ccc;
        }
        &::after {
          content: '|';
          position: absolute;
          right: -3px;
          color: #ccc;
        }
      }
    }
  }
  .process-status {
    width: 80px;
    height: 80px;
    position: absolute;
    // bottom: -8px;
    top: 6px;
    right: 6px;
    z-index: 99;
    @extend %background;
  }
  @for $i from 0 through 5 {
    .process-status#{$i} {
      background-image: url('~@/assets/auditProcess/process_status_#{$i}.png');
    }
  }
  &::v-deep {
    .el-timeline-item__tail {
      border-left-color: #DBE4FF!important;
      left: 6px;
      margin-top: 12px;
    }
    .el-timeline-item:last-child {
      .el-timeline-item__tail {
        display: none!important;
      }
    }
    .el-timeline {
      padding: 12px 10px 0 2px;
      .el-timeline-item {
        padding-bottom: 12px;
      }
      .el-timeline-item__wrapper {
        padding-left: 20px;
      }
    }
    // .timeline-item-active {
    //   .el-timeline-item__tail {
    //     border-color: #406EFF!important;
    //   }
    // }
  }
  .node-end,
  .node-begin,
  .node-ing,
  .node-comment,
  .node-checkField,
  .node-unexecuted {
    &::v-deep {
      .el-timeline-item__node {
        width: 16px;
        height: 16px;
        background-color: transparent!important;
        background-repeat: no-repeat;
        background-position: center;
        background-size: 100%;
        margin-top: 12px;
      }
    }
  }
  .node-end {
    &::v-deep {
      .el-timeline-item__node {
        background-image: url('~@/assets/auditProcess/status4.png');
      }
    }
  }
  .node-begin {
    &::v-deep {
      .el-timeline-item__node {
        background-image: url('~@/assets/auditProcess/status1.png');
      }
    }
  }
  .node-ing {
    &::v-deep {
      .el-timeline-item__node {
        background-image: url('~@/assets/auditProcess/status3.png');
      }
    }
  }
  .node-checkField {
    &::v-deep {
      .el-timeline-item__node {
        background-image: url('~@/assets/auditProcess/status8.png');
        margin-top: 17px;
      }
    }
  }
  .node-comment {
    &::v-deep {
      .el-timeline-item__node {
        //width: 9px !important;
        //height: 9px !important;
        //transform: translate(2px, 8px);
        //left: 0;
        //background-color: #AEB6C7 !important;
        background-image: url('~@/assets/auditProcess/status7.png');
        margin-top: 17px;
      }
    }
  }
  .node-unexecuted {
    &::v-deep {
      .el-timeline-item__node {
        background-image: url('~@/assets/auditProcess/status6.png');
      }
    }
  }
}
.check-filed-item {
  display: flex;
  align-items: center;
  background: #FFFFFF;
  border-radius: 8px;
  padding: 12px;
  box-shadow: 0px 3px 6px #dde1ea;
  .avatar {
    height: 28px;
    width: 28px;
    margin-right:8px;
    flex-shrink: 0;
    border-radius: 100%;
    overflow: hidden;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .label {
    color: #333;
    font-size: 14px;
    flex-shrink: 0;
    margin-right: 8px;
    max-width: 30%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .mr-2px {
    margin-right: 2px;
  }
  .fiedlName {
    color: #fd8546;
    flex:1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .time {
    color: #999;
    font-size: 12px;
  }
}
</style>
